#include "../head.h"

#define N 5
int total = 0;
int  array[N][N] = {10,20,30,40,50,
                    11,22,33,44,55,
                    66,77,88,99,11,
                    12,31,12,34,45,
                    12,43,45,66,77};
/*
    定义一个全局变量 total 累计 所有元素的和
    每一条线程 负责 计算 一行 的 元素 和 ，最后 所有线程的元素和 相加 就是 整形矩阵(二维整形数组) 所有元素和
*/
pthread_mutex_t mutex;

void *thread_func(void *arg)
{   
    int num = 0;
    for (int i = 0; i < N; i++)
    {
        num += array[*((int *)arg)][i];
    }
    pthread_mutex_lock(&mutex);
    total += num;
    pthread_mutex_unlock(&mutex);
}

int main()
{
    printf("main\n");
    pthread_mutex_init(&mutex,NULL);
    pthread_t thread[N];
    int row[N];
    for (int i = 0; i < N; i++)
    {
        row[i] = i;
        pthread_create(&thread[i],NULL,thread_func,&row[i]);
    }
    

    for (int i = 0; i < N; i++)
    {
        pthread_join(thread[i],NULL);
    }
    printf("total = %d\n",total);


    return 0;
}
